package com.xbongbong.pro.statistic.service.impl;

import com.xbongbong.paas.enums.PlatFormEnum;
import com.xbongbong.paas.pojo.dto.IsActiveAliyunCallDTO;
import com.xbongbong.paas.pojo.vo.IsActiveAliyunCallVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.statistic.enums.CloudCodeChartEnum;
import com.xbongbong.pro.statistic.manager.feign.AliyuncallCenterFeignClient;
import com.xbongbong.pro.statistic.pojo.vo.ChartCategoryListVO;
import com.xbongbong.pro.statistic.service.AliyuncallCenterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service("aliyuncallCenterService")
public class AliyuncallCenterServiceImpl implements AliyuncallCenterService {
    private static final Logger LOG = LoggerFactory.getLogger(AliyuncallCenterServiceImpl.class);
    @Resource
    AliyuncallCenterFeignClient aliyuncallCenterFeignClient;
    @Resource
    private ProBaseConfig proBaseConfig;
    @Override
    public IsActiveAliyunCallVO isActiveAliyunCall(String corpid, String userId) throws XbbException {
        IsActiveAliyunCallVO vo = new IsActiveAliyunCallVO();
        vo.setActiveAliyunCall(false);
        XbbResponse<IsActiveAliyunCallVO> response = new XbbResponse<>(vo);
        try {
            IsActiveAliyunCallDTO isActiveAliyunCallDTO = new IsActiveAliyunCallDTO();
            isActiveAliyunCallDTO.setCorpid(corpid);
            isActiveAliyunCallDTO.setUserId(userId);
            isActiveAliyunCallDTO.setPlatform(PlatFormEnum.WEB.getValue());
            String lang = LocaleContextHolder.getLocale().toString();
            response = aliyuncallCenterFeignClient.isActiveAliyunCall(isActiveAliyunCallDTO, lang);
            if (!Objects.equals(response.getCode(), ErrorCodeEnum.API_SUCCESS.getCode())) {
//            throw new XbbException(response.getCode(), response.getMsg());
                return new IsActiveAliyunCallVO();
            }
        }catch (Exception e){
        }
        return response.getResult();
    }

    @Override
    public ChartCategoryListVO getChartCategoryList() throws XbbException {
        CloudCodeChartEnum aliyunCenter = CloudCodeChartEnum.getParentByType("AliyunCenter");
        ChartCategoryListVO parent = new ChartCategoryListVO();
        long id = DateUtil.getLong();
        parent.setAlias(aliyunCenter.getAlias());
        parent.setName(aliyunCenter.getName());
        parent.setId(id);
        parent.setParentId(0L);
        List<ChartCategoryListVO> list = new ArrayList<>();
        parent.setChildList(list);
        List<CloudCodeChartEnum> childEnums = CloudCodeChartEnum.getChildListByType("AliyunCenter");
        for(CloudCodeChartEnum tempEnum:childEnums){
            ChartCategoryListVO child = new ChartCategoryListVO();
            child.setId((long) child.hashCode());
            child.setName(tempEnum.getName());
            child.setAlias(tempEnum.getAlias());
            String url = proBaseConfig.getAliyunCallcenterChartUrl();
            child.setUrl(url+tempEnum.getUrl());
            child.setParentId(id);
            parent.getChildList().add(child);
        }
        return parent;
    }
}
